﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalProject.Services.Interfaces;
using FinalProject.Web.ViewModels;
using FinalProject.Entities;
using FinalProject.Services;

namespace FinalProject.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class UserController : Controller
    {
        private readonly IUserService _userService;
        private readonly ICompanyService _companyService;
        private readonly IUserRoleService _userRoleService;

        public UserController(IUserService userService, ICompanyService companyService, IUserRoleService userRoleService)
        {
            this._userService = userService;
            this._companyService = companyService;
            this._userRoleService = userRoleService;
        }

        [HttpGet]
        public ActionResult List()
        {
            var list = _userService.GetAll().Entities;
            if (list.Any())
                return View("List", list);
            return View(list.ToList());
        }

        [HttpGet]
        public ActionResult Create()
        {
            var userViewModel = new CreateUserViewModel();
            userViewModel.Companies = _companyService.GetAll().Entities.ToList();
            userViewModel.InitializeAssignedUserRoles(_userRoleService.GetAll().Entities.ToList(), new List<UserRole>());
            return View(userViewModel);
        }

        [HttpPost]
        public ActionResult Create(CreateUserViewModel userViewModel)
        {
            var user = new User();
            user.Comment = userViewModel.Comment;
            user.CreationDate = DateTime.Now;
            user.Email = userViewModel.Email;
            user.IsApproved = true;
            user.IsLockedOut = false;
            user.IsOnline = false;
            user.Password = Helper.HashPassword(userViewModel.Password);
            user.ProviderName = "CustomMembershipProvider";
            user.UserName = userViewModel.UserName;

            var userDetails = new UserDetails();
            userDetails.CompanyId = userViewModel.CompanyId;
            userDetails.DateCreated = DateTime.Now;
            userDetails.LastUpdated = DateTime.Now;

            user.UserDetails = userDetails;

            user.UserRoles = userViewModel.ConvertAssignedRolesToUserRoles();

            _userService.Create(user);

            return RedirectToAction("List", "User");
        }

        [HttpGet]
        public ActionResult Edit(int id = 0)
        {
            try
            {
                var userEntity = _userService.GetUserWithRolesAndUserDetails(id);
                var allRoles = _userRoleService.GetAll().Entities.ToList();
                var userViewModel = new CreateUserViewModel(userEntity);
                userViewModel.Companies = _companyService.GetAll().Entities.ToList();
                userViewModel.InitializeAssignedUserRoles(allRoles, userEntity.UserRoles.ToList());
                if (userEntity.UserDetails != null)
                    userViewModel.CompanyId = userEntity.UserDetails.CompanyId;
                return View(userViewModel);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "User", "Edit");
                return View("Error", model);
            }
        }

        [HttpPost]
        public ActionResult Edit(CreateUserViewModel userViewModel)
        {
            var user = _userService.GetUserWithRolesAndUserDetails(userViewModel.UserId);

            if (!String.IsNullOrWhiteSpace(userViewModel.Comment))
                user.Comment = userViewModel.Comment;
            user.UserName = userViewModel.UserName;
            user.Email = userViewModel.Email;

            var userDetails = new UserDetails();
            userDetails.CompanyId = userViewModel.CompanyId;
            userDetails.LastUpdated = DateTime.Now;
            userDetails.DateCreated = user.CreationDate;

            user.UserDetails = userDetails;
            var newRoles = userViewModel.ConvertAssignedRolesToUserRoles();
            newRoles = _userRoleService.GetAllRolesWithId(newRoles).ToList();
            _userService.Update(user,newRoles);

            return RedirectToAction("List", "User");

        }
    }
}
